/**
 * Created by LinJe on 2015/9/17.
 * 首页发布功能
 */
(function( $ ) {

    /**
     * ****结构预览****
     * 1.上半部分：抓取及处理图片列表
     * 2.下半部分：商品描述限制字数及发布
     */


    //1.上半部分：抓取及处理图片列表
    var grabBox = (function() {

        var oGrabForm = $('.js-grab'),
            oGrabInput = oGrabForm.find('input'),
            oGrabButton = oGrabForm.find('button'),
            oProgressForm = $('.js-progress'),
            oProgressInput = oProgressForm.find('input'),
            oProgressButton = oProgressForm.find('button'),
            oProgressBar = oProgressForm.find('.progress-bar'),
            oPublishForm = $('.js-publish'),
            oPublishPush = $('#js-pubulish-button'),
            oPublishCancel = $('#js-publish-cancel'),
            oPublishImg = oPublishForm.find('.img img'),
            oPublishH2 = oPublishForm.find('h2');

        var aTmpData; //缓存抓取到的数据

        //点击抓取
        oGrabButton.on('click', function() {
            var url = oGrabInput.val();
            if ( userServers.getUserInfo().userId ) { //判断用户是否已经登陆
                //抓取数据
                if (url) {
                    grabStart(url); //开始抓取数据
                } else {
                    window.globalLayer.openWarning({'autoClose': true, time: 2000, text: '请输入商品网址'});
                }
            }else {
                    window.globalLayer.openLogin(); //打开登陆提示弹窗
                }
        });


        //开始抓取
        function grabStart( url ) {
            var iNow = 0;

            oGrabForm.hide();
            oProgressForm.show();
            oProgressInput.val( oGrabInput.val() );

            oProgressBar.css('width', '0');
            function getPercent() {
                publishServer.drabData({
                    'url': url
                }, function(msg) {
                    if ( !msg.error ) {
                        //抓取过程中请求成功
                        if ( msg.status == '0' ) {
                            progress(msg.data.percent);
                            getPercent();
                        } else if(msg.status == '1' ) {
                            grabComplete(msg.data);
                        } else {
                            window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                            cancelReturn();
                        }
                    } else {
                    //    //抓取过程中出现请求失败的情况处理
                       window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                        cancelReturn();
                    }
                });
            }
            getPercent();

            /*
             clearInterval(timerGrabing);
             timerGrabing = setInterval(function() {
             if ( iNow < 100 ) {
             iNow ++;
             progress(iNow+'%');
             } else {
             clearInterval(timerGrabing);
             grabComplete();
             }
             }, 10);
             */
        }

        function cancelReturn(){
            oPublishForm.hide();
            oProgressForm.hide();
            oGrabForm.show();
        }

        //点击取消
        oPublishCancel.on('click', function() {
            cancelReturn();
        });



        //点击发布
        oPublishPush.on('click', function() {
            if ( userServers.getUserInfo().userId ) { //判断用户是否已经登陆
                publishServer.publishData({
                    'picId': aTmpData.pics.id,
                    'productId': aTmpData.productId
                }, function(msg) {
                    //发布之后的回调函数
                    if ( msg.code == 200 ) {
                        //发布成功
                        var content = '', linkText = '';
                        switch ( msg.detail.status ) {
                            case '1':
                                content = '分享成功！';
                                linkText = '前往讨论页';
                                break;
                            case '2':
                                content = '已经有人发过此商品了';
                                linkText = '去看看';
                        }
                        window.globalLayer.openPublish({
                            'content': content,
                            'linkText': linkText,
                            'url': msg.detail.productUrl,
                            'callBack': function() {
                                //关闭弹窗之后的回调回调
                                oPublishForm.hide();
                                oGrabForm.show();
                                oGrabInput.val('');
                            }
                        });
                    } else {
                        //发布失败
                        window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                    }
                });
            } else {
                window.globalLayer.openLogin(); //打开登陆提示弹窗
            }
        });

        //抓取完成
        function grabComplete( data ) {
            oProgressForm.hide();
            oPublishForm.show();

            oPublishImg.attr('_id', data.pics.id);
            oPublishImg.attr("src",data.pics.url+"?imageMogr2/thumbnail/646x646");
            oPublishImg.attr("prodId",data.productId);
            oPublishH2.text( data.productName );
            aTmpData = data;
        }

        //抓取的进度条
        function progress( percent ) {
            oProgressBar.css('width', percent);
        }

    })();

})( jQuery );